
package org.jma.app.ewisdom.services.utils;

import java.io.IOException;

import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocket;

import org.jma.app.ewisdom.entities.FileDescriptor;
import org.jma.app.ewisdom.services.SrvFileManager;
import org.jma.lib.soap.HttpRequest;
import org.jma.lib.soap.HttpResponse;
import org.jma.lib.utils.http.FileUploadedEvent;
import org.jma.lib.utils.http.FileUploadedListener;
import org.jma.lib.utils.http.FileUploader;
/**
 * EwisdomUploader class creates a new thread that uploads a File
 * @author jesmari@ono.com
 * @version 1.0
 * @since 1.0
 */
public class EwisdomUploader extends FileUploader implements Runnable {
	

	Thread t;
	private SSLSocket ssl_s=null;
	private FileDescriptor fd=null;
	private int sslPort;

	/**
	 * EwisdomUploader constructor.
	 * @param params params[0] must contain the default repository url
	 */
	public EwisdomUploader(String[] params){	
		super.setRepository((String)params[0]);
	}
	

	/**
	 * FileUploader construcor
	 * @param sslPort ssl port
	 * @param fd a fileDescriptor to store the name and location of an uploaded file
	 * @param repository folder to store the file
	 * @throws IOException
	 */
	
	public EwisdomUploader(int sslPort, FileDescriptor fd,String repository) throws IOException{
		
		super.setAuxField(String.valueOf(fd.hashCode()));
		super.setRepository(repository);
		this.fd=fd;
		this.sslPort=sslPort;
		t = new Thread(this);  
	}
	/*
	 * TODO cancel upload if no receives nothing in a defined time
	 */
	public void run(){
		java.io.InputStream dataFromClient=null;
		java.io.OutputStream dataToClient=null;
		SSLServerSocket sslserversocket=null;
		try{
			SSLServerSocketFactory sslserversocketfactory =
				(SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
			sslserversocket =
				(SSLServerSocket) sslserversocketfactory.createServerSocket(sslPort);

			System.out.println("Listening port:"+sslPort );
			SSLSocket sslsocket = (SSLSocket) sslserversocket.accept(); // Blocks waiting for a new connection
			ssl_s=sslsocket;
			dataFromClient = ssl_s.getInputStream();
			dataToClient = ssl_s.getOutputStream();
			HttpRequest httpRequest= new HttpRequest(dataFromClient);
			HttpResponse httpResponse = new HttpResponse (dataToClient);
			httpRequest.setInetAddress(ssl_s.getInetAddress());
			String fileName=doPost(httpRequest, httpResponse);
			
		}catch (Exception e){
			e.printStackTrace();
		}finally{
			try{
				dataFromClient.close();
				dataToClient.close();
				if(sslserversocket!=null) sslserversocket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally{
				
				SrvFileManager.uploaders[this.sslPort-SrvFileManager.FIRST_AVAILABLE_PORT]=null;
			}
		}
	}
	/**
	 * Starts the run method of the uploader thread
	 */
	public void start(){
		t.start(); // Start the child process
	}
	
	/**
	 * gets the FileDescriptor
	 * @return a FileDescriptor
	 */
	public FileDescriptor getFileDescriptor(){
		return fd;
	}
	/**
	 * returns the File Identification
	 * @return FID
	 */
	public String getFID(){
		return super.getAuxField();
	}

	
}
